Grokking Microsoft Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Microsoft Easy Problems
Buddy Strings (easy)
Solution: Buddy Strings
Can Place Flowers (easy)
Solution: Can Place Flowers
Reverse a LinkedList (easy)
Solution: Reverse a LinkedList
Maximum Number of Balloons (easy)
Solution: Maximum Number of Balloons
Binary Tree Path Sum (easy)
Solution: Binary Tree Path Sum
Pair with Target Sum (easy)
Solution: Pair with Target Sum
Buy Two Chocolates (easy)
Solution: Buy Two Chocolates
Root Equals Sum of Children (easy)
Solution: Root Equals Sum of Children
Excel Sheet Column Title (easy)
Solution: Excel Sheet Column Title
Merge Strings Alternately (easy)
Solution: Merge Strings Alternately
Min Cost Climbing Stairs (easy)
Solution: Min Cost Climbing Stairs
Plus One (easy)
Solution: Plus One
Intersection of Two Arrays (easy)
Solution: Intersection of Two Arrays
Find the Index of the First Occurrence in a String (easy)
Solution: Find the Index of the First Occurrence in a String
Merge Two Sorted Lists (easy)
Solution: Merge Two Sorted Lists
Nth Digit (medium)
Solution: Nth Digit
Restore IP Addresses (medium)
Solution: Restore IP Addresses
Find Original Array From Doubled Array (medium)
Solution: Find Original Array From Doubled Array
Reverse Words in a String II (medium)
Solution: Reverse Words in a String II
Longest Arithmetic Subsequence (medium)
Solution: Longest Arithmetic Subsequence
Matchsticks to Square (medium)
Solution: Matchsticks to Square
Score of Parentheses (medium)
Solution: Score of Parentheses
Maximum Length of a Concatenated String with Unique Characters (medium)
Solution: Maximum Length of a Concatenated String with Unique Characters
Inorder Successor in BST (medium)
Solution: Inorder Successor in BST
Distribute Coins in Binary Tree (medium)
Solution: Distribute Coins in Binary Tree
Spiral Matrix (medium)
Solution: Spiral Matrix
Delete Node in a BST (medium)
Solution: Delete Node in a BST
Sum of Subarray Minimums (medium)
Solution: Sum of Subarray Minimums
Rotate Image (medium)
Solution: Rotate Image
Combinations (medium)
Solution: Combinations
Maximum Level Sum of a Binary Tree (medium)
Solution: Maximum Level Sum of a Binary Tree
Multiply Strings (medium)
Solution: Multiply Strings
Best Time to Buy and Sell Stock II (medium)
Solution: Best Time to Buy and Sell Stock II
Largest Palindromic Number (medium)
Solution: Largest Palindromic Number
Remove Duplicate Letters (medium)
Solution: Remove Duplicate Letters
Next Permutation (medium)
Solution: Next Permutation
132 Pattern (medium)
Solution: 132 Pattern
Valid Sudoku (medium)
Solution: Valid Sudoku
Number of Islands (medium)
Solution: Number of Islands
Buildings With an Ocean View (medium)
Solution: Buildings With an Ocean View
Regular Expression Matching (hard)
Solution: Regular Expression Matching
First Missing Positive (hard)
Solution: First Missing Positive
Largest Rectangle in Histogram (hard)
Solution: Largest Rectangle in Histogram
Sliding Window Maximum (hard)
Solution: Sliding Window Maximum
Count Unique Characters of All Substrings of a Given String (hard)
Solution: Count Unique Characters of All Substrings of a Given String
Word Ladder (hard)
Solution: Word Ladder
Word Ladder II (hard)
Solution: Word Ladder II
Number of Flowers in Full Bloom (hard)
Solution: Number of Flowers in Full Bloom
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Longest Valid Parentheses (hard)
Solution: Longest Valid Parentheses
Solution: Plus One

Problem Statement

You are given a digits array, where digits[i] is between 0 to 9. It is given that when you combine all elements of the digits array in left to right order, it forms the large integer.

Add 1 to large integer and return the array containing the digits of the resultant large integer.

Examples

  • Example 1:

    • Input: [1, 2, 3]
    • Expected Output: [1, 2, 4]
    • Justification: The number 123 is incremented to 124. No carry over beyond the last digit.
  • Example 2:

    • Input: [4, 9, 9, 9]
    • Expected Output: [5, 0, 0, 0]
    • Justification: Incrementing 4999 results in a carry over at each digit, leading to 5000.
  • Example 3:

    • Input: [9, 8, 7, 6, 8, 9, 9, 9]
    • Expected Output: [9, 8, 7, 6, 9, 0, 0, 0]
    • Justification: Incrementing the last three digits of 98768999 by 1 results in a carry that changes these digits to 0 and increases the 5th digit by 1, resulting in 98769000.

Solution

To solve this problem, we'll iterate through the array of digits from the end towards the beginning, mimicking the process of addition in arithmetic. This approach works because incrementing by one affects only the least significant digits and may carry over to the more significant digits in a cascading manner. By starting from the end, we can efficiently manage carries and directly modify the array in place, if possible. This method is effective as it directly mirrors the manual process of addition, ensuring that all edge cases, such as carry-overs that increase the array size, are handled naturally within the algorithm's flow.

The key to this approach is handling the carry-over. If incrementing the current digit results in a value less than 10, we can stop, as no further carry will occur. However, if it equals 10, we set the current digit to 0 and carry over to the next significant digit. This process continues until we either no longer have a carry or reach the most significant digit. If we still have a carry at the most significant digit, we need to adjust the array size to accommodate the increased magnitude of the number.

Step-by-step Algorithm

  • Start iterating from the last digit of the array towards the first.
  • Increment the last digit by 1. If the result is less than 10, update the digit and return the array.
  • If the digit becomes 10 after incrementing, set it to 0 and carry over 1 to the next significant digit.
  • Continue this process moving towards the more significant digits.
  • If a carry is present when we reach the most significant digit and it results in 10, prepend 1 to the array to accommodate the carry.
  • Return the modified array as the final result.

Algorithm Walkthrough

Using the input [9, 8, 7, 6, 8, 9, 9, 9], let's illustrate the algorithm step by step:

  1. Start with the last digit (9):

    • Incrementing 9 by 1 gives 10. Set this digit to 0, carry over 1.
  2. Move to the next digit to the left (second 9):

    • Again, 9 + 1 = 10. Set this digit to 0, carry over 1.
  3. Proceed to the next digit to the left (third 9):

    • Similarly, 9 + 1 = 10. Set this digit to 0, carry over 1.
  4. Next, address the fourth digit from the right (8):

    • This time, 8 + 1 = 9. Update this digit to 9. Since the result is less than 10, no further carry is needed.
  5. Since we no longer have a carry, the process stops. The digits to the left of the one we just updated remain unchanged.

  6. The final array, after processing the increment and handling all necessary carry-overs, is [9, 8, 7, 6, 9, 0, 0, 0].

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

Time Complexity

  • : The algorithm iterates over each digit of the input array exactly once in the worst-case scenario (when all digits are 9 and need to be turned into 0, with an additional digit added at the beginning). Here, n represents the number of digits in the input array.

Space Complexity

  • : Algorithm performs the in-place addition. So, it doesn't require extra space.
Plus One (easy)
Intersection of Two Arrays (easy)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity

Space Complexity